home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 431_01 / myalloc.c < prev    next >
Text File  |  1994-05-22  |  969b  |  47 lines

  1. #include <dos.h>
  2.  
  3. #include "myalloc.h"
  4.  
  5. /*
  6.   i wrap the standard allocations around
  7.     2 bytes -- head ID 0x1234
  8.     2 bytes -- size (n)
  9.     n bytes -- data
  10.     2 bytes -- tail ID 0x4321
  11. */
  12. void *my_malloc(unsigned size)
  13. {
  14.   unsigned seg;
  15.   if (allocmem((size+15+6)/16, &seg)) {
  16.     char *buf=MK_FP(seg, 0);
  17.     *(unsigned *) buf=0x1234;
  18.     *(unsigned *) (buf+2)=size;
  19.     *(unsigned *) (4+buf+size)=0x4321;
  20.     return MK_FP(seg, 4);
  21.   } else
  22.     return 0;
  23. }
  24.  
  25. void my_free(void *buf)
  26. {
  27.   freemem(FP_SEG(buf));
  28. }
  29.  
  30. /*
  31.   check that [buf] was allocated by me, and currently points
  32.   to a valid part of the allocation
  33. */
  34. int my_check(void *buf)
  35. {
  36.   int err=0;
  37.   char *mybuf=MK_FP(FP_SEG(buf), 0);
  38.   int size=*(unsigned *) (mybuf+2);
  39.  
  40.   if (*(unsigned *) mybuf != 0x1234)
  41.     err |= 1;
  42.   if (*(unsigned *) (mybuf+4+size) != 0x4321)
  43.     err |= 2;
  44.   if ((FP_OFF(buf) < 4) || (FP_OFF(buf) > 4+size))
  45.     err |= 4;
  46.   return err;
  47. }